//
//  LXLoginView.m
//  cat
//
//  Created by imera on 16/1/19.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXLoginView.h"
#import "LXInputView.h"
#import "UIImage+Lx.h"

#define LXLoginViewMargin 15

@interface LXLoginView()
@property (nonatomic, weak) UIImageView *chatImageView;
@property (nonatomic, weak) UIImageView *catImageView;
/** 用户名输入框 */
@property (nonatomic, weak) LXInputView *userInputView;
/** 密码输入框 */
@property (nonatomic, weak) LXInputView *pwdInputView;
/** 登录按钮 */
@property (nonatomic, weak) UIButton *loginBtn;
/** 注册按钮 */
@property (nonatomic, weak) UIButton *registerBtn;
@end

@implementation LXLoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    UIImageView *chatImageView = [[UIImageView alloc]init];
    chatImageView.image = [UIImage imageNamed:@"chat"];
    [self addSubview:chatImageView];
    self.chatImageView = chatImageView;
    
    UIImageView *catImageView = [[UIImageView alloc]init];
    catImageView.image = [UIImage imageNamed:@"c"];
    [self addSubview:catImageView];
    self.catImageView = catImageView;
    
    LXInputView *userInputView = [LXInputView initWithIcon:@"user" placeholder:@"请输入用户名"];
    [self addSubview:userInputView];
    self.userInputView = userInputView;
    
    LXInputView *pwdInputView = [LXInputView initWithIcon:@"pwd" placeholder:@"请输入密码"];
    pwdInputView.isPwd = YES;
    [self addSubview:pwdInputView];
    self.pwdInputView = pwdInputView;
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setBackgroundImage:[UIImage resizeWithName:@"blue"] forState:UIControlStateNormal];
    [loginBtn setImage:[UIImage imageNamed:@"denglu"] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginBtn];
    self.loginBtn = loginBtn;
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerBtn setBackgroundImage:[UIImage resizeWithName:@"blue"] forState:UIControlStateNormal];
    [registerBtn setImage:[UIImage imageNamed:@"register"] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(register_) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:registerBtn];
    self.registerBtn = registerBtn;
}

#pragma mark - 登录
- (void)login
{
    if ([self.myDelegate respondsToSelector:@selector(loginView:userName:password:)]) {
        [self.myDelegate loginView:self userName:self.userInputView.textField.text password:self.pwdInputView.textField.text];
    }
}

#pragma mark - 注册
- (void)register_
{
    if ([self.myDelegate respondsToSelector:@selector(userRegister:)]) {
        [self.myDelegate userRegister:self];
    }
}

- (void)layoutSubviews
{
    UIImageView *chatImageView = self.chatImageView;
    CGFloat chatImageViewX = LXLoginViewMargin * 2;
    CGFloat chatImageViewY = LXLoginViewMargin * 1;
    CGFloat chatImageViewW = (self.frame.size.width - chatImageViewX * 2) * 0.66;
    CGFloat chatImageViewH = 109;
    chatImageView.frame = CGRectMake(chatImageViewX, chatImageViewY, chatImageViewW, chatImageViewH);
    
    UIImageView *catImageView = self.catImageView;
    CGFloat catImageViewX = CGRectGetMaxX(chatImageView.frame);
    CGFloat catImageViewY = chatImageViewY;
    CGFloat catImageViewW = (self.frame.size.width - chatImageViewX * 2) * 0.34;
    CGFloat catImageViewH = 96;
    catImageView.frame = CGRectMake(catImageViewX, catImageViewY, catImageViewW, catImageViewH);
    
    LXInputView *userInputView = self.userInputView;
    CGFloat userInputViewX = LXLoginViewMargin * 2;
    CGFloat userInputViewY = CGRectGetMaxY(chatImageView.frame) + LXLoginViewMargin * 2;
    CGFloat userInputViewW = (self.frame.size.width - userInputViewX * 2);
    CGFloat userInputViewH = 40;
    userInputView.frame = CGRectMake(userInputViewX, userInputViewY, userInputViewW, userInputViewH);
    
    LXInputView *pwdInputView = self.pwdInputView;
    CGFloat pwdInputViewW = userInputViewW;
    CGFloat pwdInputViewH = userInputViewH;
    CGFloat pwdInputViewX = userInputViewX;
    CGFloat pwdInputViewY = CGRectGetMaxY(userInputView.frame) + LXLoginViewMargin * 2;
    pwdInputView.frame = CGRectMake(pwdInputViewX, pwdInputViewY, pwdInputViewW, pwdInputViewH);
    
    UIButton *loginBtn = self.loginBtn;
    CGFloat loginBtnW = userInputViewW;
    CGFloat loginBtnH = userInputViewH;
    CGFloat loginBtnX = userInputViewX;
    CGFloat loginBtnY = CGRectGetMaxY(pwdInputView.frame) + LXLoginViewMargin * 2;
    loginBtn.frame = CGRectMake(loginBtnX,loginBtnY, loginBtnW, loginBtnH);
    
    UIButton *registerBtn = self.registerBtn;
    CGFloat registerBtnW = userInputViewW;
    CGFloat registerBtnH = userInputViewH;
    CGFloat registerBtnX = userInputViewX;
    CGFloat registerBtnY = CGRectGetMaxY(loginBtn.frame) + LXLoginViewMargin * 2;
    registerBtn.frame = CGRectMake(registerBtnX, registerBtnY, registerBtnW, registerBtnH);
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.userInputView hideKeyBorard];
    [self.pwdInputView hideKeyBorard];
}
@end
